package com.lsu.facade.experiment;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

/**
 * 柱状图
 *
 * @Author wang suo
 * @Date 2020/11/27 0027 10:15
 * @Version 1.0
 */
class Histogram {
    void draw() {
        //generate the dataset
        DefaultCategoryDataset dataset = getDataSet();

        //generate the chart
        JFreeChart chart = ChartFactory.createBarChart("Test", "AxisLabel",
                "ValueLabel", dataset, PlotOrientation.VERTICAL, true, true,
                false);
        CategoryPlot plot = chart.getCategoryPlot();
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        //显示条目标签
        renderer.setBaseItemLabelsVisible(true);
        //设置条目标签生成器,在JFreeChart1.0.6之前可以通过renderer.setItemLabelGenerator(CategoryItemLabelGenerator generator)方法实现，但是从版本1.0.6开始有下面方法代替
        renderer
                .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        //设置条目标签显示的位置,outline表示在条目区域外,baseline_center表示基于基线且居中
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        ChartFrame frame = new ChartFrame("柱状图", chart);
        frame.pack();
        frame.setVisible(true);
    }

    private static DefaultCategoryDataset getDataSet() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        int[] nums = new int[]{
                52,
                85,
                60,
                64,
                54,
                77,
                92,
                67,
                86,
                82,
                69,
                84,
                69,
                72,
                61,
                55,
                67,
                83,
                77,
                99,
                78,
                79,
                60,
                83,
                80,
                74,
                98,
                61,
                87,
                50,
                81,
                60,
                55,
                72,
                68,
                84,
                73,
                68,
                63,
                73,
                74,
                85,
                70,
                60
        };
        int less60 = 0, to70 = 0, to80 = 0, to90 = 0, to100 = 0, total = nums.length;
        for (int num : nums) {
            if (num < 60) {
                less60++;
            } else if (num < 70) {
                to70++;
            } else if (num < 80) {
                to80++;
            } else if (num < 90) {
                to90++;
            } else {
                to100++;
            }
        }
        dataset.addValue(less60, "less than 60", "less than 60");
        dataset.addValue(to70, "60to70", "60to70");
        dataset.addValue(to80, "70to80", "70to80");
        dataset.addValue(to90, "80to90", "80to90");
        dataset.addValue(to100, "90to100", "90to100");
        return dataset;
    }
}
